package com.tl.myapplication

import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.os.Binder
import android.provider.Settings
import android.view.View
import android.view.WindowManager
import androidx.core.net.toUri

class GlobalDialog {
    companion object {
        val instance: GlobalDialog by lazy { GlobalDialog() }
    }

    fun init(view: View) {
        val intent = Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            "package:${App.instance.packageName}".toUri()
        )
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        App.instance.startActivity(intent)
        val flag = (WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
                or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                or WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
                or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                or WindowManager.LayoutParams.FLAG_DIM_BEHIND
                or WindowManager.LayoutParams.FLAG_FULLSCREEN
                or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                or WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

        val wl = WindowManager.LayoutParams(
            300,
            600,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            flag,
            PixelFormat.TRANSLUCENT
        )
        wl.token = Binder()
        wl.packageName = App.instance.packageName

        val wp = App.instance.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wp.addView(view, wl)
    }
}